package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SaleBean;
import com.project.bean.SupplierBean;
import com.project.mapper.ISupplierMapper;
import com.project.service.ISaleService;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;


    @Override
    public IPage<SupplierBean> cutByItem(Integer pageNO, String name, String scope) {

        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();

        if (name != null && !"".equals(name)) {
            qw.like("s_name", name);
        }
        if (scope != null && !"".equals(scope)) {
            qw.like("s_scope", scope);
        }

        return supplierMapper.selectPage(new Page<>(pageNO, 5), qw);
    }

    @Override
    public SupplierBean findById(Integer id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String person, String phone) {
        SupplierBean supplier = supplierMapper.selectById(id);
        supplier.setPerson(person);
        supplier.setPhone(phone);

        supplierMapper.updateById(supplier);
    }

    @Override
    public void add(SupplierBean supplier) {
        supplierMapper.insert(supplier);

    }

    @Override
    public String addSupplierList(List<SupplierBean> supplierList) {
        int success = 0;

        for (SupplierBean supplier : supplierList) {
            QueryWrapper<SupplierBean> qw = new QueryWrapper<>();

            qw.eq("s_name", supplier.getName());
            if (supplierMapper.selectOne(qw) == null) {
                supplierMapper.insert(supplier);
                success++;
            }
        }

        return "上传"+supplierList.size()+"条，成功"+success+"条";
    }

    @Override
    public List<SupplierBean> findAll() {
        return supplierMapper.selectList(null);
    }
}
